angular.module('CourseApp', [
	'mobile-angular-ui',
	'CourseApp.controllers',
	'CourseApp.controllers.Main',
	'CourseApp.controllers.Modal',
	'CourseApp.directive',
	'CourseApp.services',
	'ngResource',
	'ngCookies',
	'ngTouch',
	'ui.router',
	'toastr',
	'ngAnimate',
	'LocalStorageModule'
])
	.config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider, $cookieStoreProvider, toastrConfig, localStorageServiceProvider) {
		localStorageServiceProvider.setStorageType('localStorage');

		$urlRouterProvider.otherwise('/home');
		$stateProvider
			.state('home', {
				url: '/home',
				views: {
					'navView': {
						templateUrl: 'nav.main.html'
					},
					'contentView': {
						templateUrl: 'home.html'
					}
				}
			})
			.state('course', {
				url: '/course',
				views: {
					'navView': {
						templateUrl: 'nav.main.html'
					},
					'contentView': {
						templateUrl: 'course.html'
					}
				}
			})
			.state('course.list', {
				url: '/list',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						controller: 'CourseCtrl',
						templateUrl: 'course.list.html'
					}
				}
			})
			.state('course.detail', {
				url: '/detail/{scId:[0-9]*$}',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						controller: 'CourseDetailCtrl',
						templateUrl: 'course.detail.html'
					}
				}
			})
			.state('course.public', {
				url: '/public/{couId:[0-9]*$}',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						controller: 'CourseNoticeCtrl',
						templateUrl: 'course.public.html'
					}
				}
			})
			.state('course.examlist', {
				url: '/examlist',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						templateUrl: 'course.examlist.html'
					}
				}
			})
			.state('course.examdetail', {
				url: '/examdetail',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						templateUrl: 'course.examdetail.html'
					}
				}
			})
			.state('question', {
				url: '/question/:couId',
				views: {
					'navView': {
						templateUrl: 'nav.main.html'
					},
					'contentView': {
						templateUrl: 'question.html'
					}
				}
			})
			.state('question.list', {
				url: '/list',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						controller: 'QuestionListCtrl',
						templateUrl: 'question.list.html'
					}
				}
			})
			.state('question.detail', {
				url: '/detail/:queId',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						controller: 'QuestionDetailCtrl',
						templateUrl: 'question.detail.html'
					}
				}
			})
			.state('question.detail.edit', {
				url: '/edit',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'@question': {
						controller: 'QuestionEditCtrl',
						templateUrl: 'question.edit.html'
					}
				}
			})
			.state('question.add', {
				url: '/add',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'': {
						controller: 'QuestionAddCtrl',
						templateUrl: 'question.add.html'
					}
				}
			})
			.state('question.detail.answer', {
				url: '/answer/:ansId',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'@question': {
						controller: 'AnswerDetailCtrl',
						templateUrl: 'question.detail.answer.html'
					}
				}
			})
			.state('question.detail.answer.edit', {
				url: '/edit',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'@question': {
						controller: 'AnswerEditCtrl',
						templateUrl: 'question.detail.answer.edit.html'
					}
				}
			})
			.state('question.detail.answeradd', {
				url: '/answeradd',
				views: {
					'navView@': {
						templateUrl: 'nav.main.html'
					},
					'@question': {
						controller: 'AnswerAddCtrl',
						templateUrl: 'question.detail.answer.add.html'
					}
				}
			});

		/* Register error provider that shows message on failed requests or redirects to login page on
		 * unauthenticated requests */
		$httpProvider.interceptors.push(function($q, $rootScope, $location, $cookieStore, localStorageService) {
			return {
				'responseError': function(rejection) {
					var status = rejection.status;
					var config = rejection.config;
					var method = config.method;
					var url = config.url;

					if (status == 401) {
						$location.path("/home");
						$rootScope.student = undefined;
						$rootScope.authToken = undefined;
						localStorageService.remove('student', 'authToken');
					} else {
						$rootScope.error = method + " on " + url + " failed with status " + status;
					}

					return $q.reject(rejection);
				}
			};
		});

		/* Registers auth token interceptor, auth token is either passed by header or by query parameter
		 * as soon as there is an authenticated user */
		$httpProvider.interceptors.push(function($q, $rootScope, $location, $cookieStore, localStorageService) {
			return {
				'request': function(config) {
					var isRestCall = true;
					// console.log($rootScope.authToken);
					$rootScope.authToken = localStorageService.get('authToken');
					if (isRestCall && angular.isDefined($rootScope.authToken)) {
						var authToken = $rootScope.authToken;

						if (true) {
							config.headers['X-Auth-Token'] = authToken;
						} else {
							config.url = config.url + "?token=" + authToken;
						}
					}
					return config || $q.when(config);
				}
			}
		});

		angular.extend(toastrConfig, {
			allowHtml: true,
			autoDismiss: true,
			containerId: 'toast-container',
			extendedTimeOut: 1000,
			iconClasses: {
				error: 'toast-error',
				info: 'toast-info',
				success: 'toast-success',
				warning: 'toast-warning'
			},
			maxOpened: 0,
			messageClass: 'toast-message',
			newestOnTop: true,
			onHidden: null,
			onShown: null,
			positionClass: 'toast-bottom-center',
			preventDuplicates: false,
			preventOpenDuplicates: false,
			progressBar: false,
			tapToDismiss: true,
			target: 'body',
			templates: {
				toast: 'directives/toast/toast.html',
				progressbar: 'directives/progressbar/progressbar.html'
			},
			timeOut: 1000,
			titleClass: 'toast-title',
			toastClass: 'toast'
		});
	});